前言:花了近两天的时间,终于把Ghost博客搭建好了。刚开始VPS装的是CentOS 6,按照网上的教程搭建好了Ghost,搭好后发现Ghost版本不是最新版,然后手贱的想升级,按照官方文档操作了半天,都没有升级成功,然后就重装了系统(还是CentOS 6),再装Ghost的时候确一直报错~。不得已又重装了Ubuntu 14.04系统。记录一下搭建的过程,备忘。
一、安装Node.js
引用PPA脚本安装
sudo curl -sL https://deb.nodesource.com/setup_4.x | sudo bash -
PS全新的系统可能需要安装 sudo
和 curl
命令。
导入完毕以后,直接安装 Node.js
sudo apt-get install nodejs
这个 Node.js
的包已经包含了 npm
所以你不用单独安装 npm
,而有些 Node.js
程序还得单独安装 build-essentials
包,安装一下:
sudo apt-get install build-essential
安装完毕以后查看一下 Node.js 和 npm 的版本
node -v
npm -v
PS Ghost 需要运行在 0.10.*
的 Node.js 版本。
#####二、安装Ghost程序
基于安全考虑,可以新建立个 ghost
用户,这里安装在 /var/www/
目录下
useradd ghost
mkdir /var/www
cd /var/www
curl -L https://ghost.org/zip/ghost-latest.zip -o ghost.zip
unzip ghost.zip
chown -R ghost:ghost /var/www
PS 新系统可能需要安装unzip
命令。
解压缩完成后开始安装运行
npm install --production
npm
安装完成后,先用开发者模式启动Ghost.
npm start
PS 这里一定要确认能正常运行,我的刚开始就是这里失败,结果全部搭建好了后一直是 502 Bad Gateway.
Ctrl + C
停止运行,编辑config.js
文件
vi config.js
把 url: 'http://my-ghost-blog.com'
, 这行改成你的域名,比如 url: https://chonghe.org'
,保存并退出。
Ghost 保持后台运行,安装 pm2。
npm install pm2 -g
运行Ghost
NODE_ENV=production pm2 start index.js --name "Ghost"
#####三、安装Nginx并设置反向代理
先安装
sudo add-apt-repository ppa:nginx/stable
sudo apt-get update
sudo apt-get install nginx-extras
修改
vi /etc/nginx/sites-available/default
server {
listen 80;
server_name chonghe.org;
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header Host $host;
proxy_pass http://127.0.0.1:2368;
}
location ~* \.(?:ico|css|js|gif|jpe?g|png|ttf|woff)$ {
access_log off;
expires 30d;
add_header Pragma public;
add_header Cache-Control "public, mustrevalidate, proxy-revalidate";
proxy_pass http://127.0.0.1:2368;
}
location = /robots.txt { access_log off; log_not_found off; }
location = /favicon.ico { access_log off; log_not_found off; }
location ~ /\.ht {
deny all;
}
}
保存并重启 Nginx
sudo service nginx restart
完毕。
参考文档: